Satan stood up against Israel and incited David to count [the people of] Israel.
References for 1 Chronicles 21:1
So David said to Joab and the commanders of the troops, "Go and count Israel from Beer-sheba to Dan and bring [a report] to me so I can know their number."
Joab replied, "May the Lord multiply the number of His people a hundred times over! My lord the king, aren't they all my lord's servants? Why does my lord want to do this? Why should he bring guilt on Israel?"
Yet the king's order prevailed over Joab. So Joab left and traveled throughout Israel and then returned to Jerusalem.
Joab gave David the total of the registration of the troops. In all Israel there were 1,100,000 swordsmen and in Judah itself 470,000 swordsmen.
But he did not include Levi and Benjamin in the count because the king's command was detestable to him.
This command was also evil in God's sight, so He afflicted Israel.
David said to God, "I have sinned greatly because I have done this thing. Now, because I've been very foolish, please take away Your servant's guilt."
References for 1 Chronicles 21:8
Then the Lord instructed Gad, David's seer,
References for 1 Chronicles 21:9